package com.ibangnuo.manage.service.alipay;

import com.ibangnuo.framework.domain.Refund;
import com.ibangnuo.framework.domain.request.alipay.RefundRequest;
import com.ibangnuo.framework.model.response.CommonCode;
import com.ibangnuo.framework.model.response.ObjectResult;
import com.ibangnuo.framework.model.response.QueryResponseResult;
import com.ibangnuo.framework.model.response.QueryResult;
import com.ibangnuo.manage.dao.alipay.RefundRepository;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;

/**
 * @Author: wwy
 * @Date: 2019-12-22 13:43
 */
@Service
public class RefundService {

    private static Logger LOGGER = LoggerFactory.getLogger(RefundService.class);
    @Autowired
    private RefundRepository refundRepository;
    /**
     * 分页条件查询退款单
     * @param page
     * @param size
     * @param refundRequest
     * @return
     */
    public QueryResponseResult findList(int page, int size, RefundRequest refundRequest){
        if(refundRequest == null){
            return new QueryResponseResult(CommonCode.INVALID_PARAM,null);
        }
        if(refundRequest.getDateTime()==null){
            refundRequest.setDateTime(new Date());
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String startTimeStr = simpleDateFormat.format(refundRequest.getDateTime());
            Date startTime = simpleDateFormat.parse(startTimeStr);
            long time = startTime.getTime();
            long endTimeLong = time+(24*60*60*1000);
            Date endTime = new Date(endTimeLong);
            Specification specification = (var1, var2, var3)->{
                Predicate predicate = var3.conjunction();
                if(StringUtils.isNotEmpty(refundRequest.getSnNum())){
                    predicate.getExpressions().add(var3.equal(var1.get("sn"), refundRequest.getSnNum()));
                }
                //状态
                predicate.getExpressions().add(var3.equal(var1.get("status"), 1));
                if(StringUtils.isNotEmpty(refundRequest.getOutTradeNo())){
                    predicate.getExpressions().add(var3.equal(var1.get("outTradeNo"), refundRequest.getOutTradeNo()));
                }
                //起始日期
                predicate.getExpressions().add(var3.greaterThanOrEqualTo(var1.get("createTime").as(Date.class), startTime));
                //结束日期
                predicate.getExpressions().add(var3.lessThanOrEqualTo(var1.get("createTime").as(Date.class), endTime));
                return predicate;
            };
            if(page<=0){
                page = 1;
            }
            page = page - 1;
            if(size<=0){
                size = 20;
            }
            Pageable pageable = PageRequest.of(page,size);
            Page all = refundRepository.findAll(specification, pageable);
            QueryResult queryResult = new QueryResult();
            queryResult.setTotal(all.getTotalElements());
            queryResult.setList(all.getContent());
            return new QueryResponseResult(CommonCode.SUCCESS,queryResult);
        } catch (ParseException e) {
            LOGGER.error("时间转换异常:{}",e.getMessage());
        }
        return new QueryResponseResult(CommonCode.FAIL,null);
    }

    /**
     * 根据id查找
     * @param id
     * @return
     */
    public ObjectResult findById(String id){
        Optional<Refund> optional = refundRepository.findById(id);
        if(optional.isPresent()){
            return new ObjectResult(CommonCode.SUCCESS,optional.get());
        }
        return new ObjectResult(CommonCode.FAIL,null);
    }
}
